import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseInterceptors,
  UploadedFile
} from '@nestjs/common'
import * as xlsx from 'xlsx'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import { ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { AwardRuleService } from './award-rule.service'
import { CreateAwardRuleDto } from './dto/create-award-rule.dto'
import { UpdateAwardRuleDto } from './dto/update-award-rule.dto'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('奖惩规则')
@Controller('award-rule')
export class AwardRuleController {
  constructor(private readonly awardRuleService: AwardRuleService) {}

  @Post()
  @ApiOperation({ summary: '创建奖惩规则' })
  @ApiBody({ type: CreateAwardRuleDto, description: '奖惩规则信息' })
  async create(@Body() createAwardRuleDto: CreateAwardRuleDto) {
    return FResponse.success(
      await this.awardRuleService.create(createAwardRuleDto)
    )
  }
  @Post('upload')
  @ApiOperation({ summary: '批量创建奖惩规则' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateAwardRuleDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.awardRuleService.create(item)))
    return FResponse.success('批量创建成功')
  }
  @Post('all')
  @ApiOperation({ summary: '获取所有奖惩规则' })
  @ApiBody({
    description: '奖惩规则信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      type?: string
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.awardRuleService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        type: body.type,
        isDelete: body.isDelete
      })
    )
  }
  // 获取真全部奖惩规则
  @Get('all')
  @ApiOperation({ summary: '获取真全部奖惩规则' })
  async findAllOption() {
    return FResponse.success(await this.awardRuleService.findAllOption())
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改指定奖惩规则' })
  @ApiParam({ name: 'id', description: '奖惩规则ID' })
  @ApiBody({ type: UpdateAwardRuleDto, description: '奖惩规则信息' })
  async update(
    @Param('id') id: string,
    @Body() updateAwardRuleDto: UpdateAwardRuleDto
  ) {
    return FResponse.success(
      await this.awardRuleService.update(+id, updateAwardRuleDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除奖惩规则' })
  @ApiBody({ type: [Number], description: '奖惩规则ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.remove(ids))
  }

  // 彻底删除
  @Delete('thorough')
  @ApiOperation({ summary: '批量彻底删除奖惩规则' })
  @ApiBody({ type: [Number], description: '奖惩规则ID数组' })
  async batchThoroughRemove(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.thoroughRemove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复奖惩规则' })
  @ApiBody({ type: [Number], description: '奖惩规则ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.restore(ids))
  }
}
